package cpa.prowon.gui.actions;

import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.image.ImageContainer;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.program.Program;

/**
 * Launches an image with the operating system associated application
 * @author Chris
 */
public class OpenAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author
	 */
	public OpenAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_open";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_edit_open_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "file:icons/run.gif";
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Get the selected image
		IStructuredSelection selection = window.getPictureListSelection();
		ImageContainer selected_image = (ImageContainer) selection.getFirstElement();
		
		// Launch the associated application
		String fileName = selected_image.getPathName();
		Program.launch(fileName);
		
	}
	
	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 * 
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getPictureListSelection();
		if (selection.size() != 1) {
			setEnabled(false);
			return;
		}
		ImageContainer selected_image = (ImageContainer) selection.getFirstElement();
		setText(Application.getInstance().getResourceBundle().getString("menu_item_edit_open") + " " + selected_image.getFileName());
		setEnabled(true);

	}
	
}
